package com.czy.owner.ui.archive;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.adapter.OrderMultiStyleAdapter2;
import com.czy.owner.adapter.TestingListAdapter;
import com.czy.owner.api.OwneiInsListApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.TestingModel;
import com.czy.owner.entity.TextingListModel;
import com.czy.owner.entity.WorkOrderModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.net.utils.JsonUtil;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TestingListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_subtitle)
    TextView toolbarSubtitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    private TestingListAdapter testingListAdapter;
    private Handler handler = new Handler();
    private int page = 1;
    private List<TextingListModel> dataList = new ArrayList<>();

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_testing_list;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        toolbarTitle.setText("车检记录");
        testingListAdapter = new TestingListAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(testingListAdapter);
        recyclerView.setAdapterWithProgress(testingListAdapter);

        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, PhoneUtils.dip2px(this, 2), this.getResources().getColor(R.color.appThemeBackgroundColor)));
        View view = LayoutInflater.from(this).inflate(R.layout.order_empty_view, (ViewGroup) this.getWindow().getDecorView(), false);
        TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_content.setText("您还没有相关车检记录");
        recyclerView.setEmptyView(view);
        recyclerView.setErrorView(R.layout.view_error_refresh);
        testingListAdapter.setMore(R.layout.view_more, this);
        testingListAdapter.setNoMore(R.layout.view_nomore);
        recyclerView.getErrorView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recyclerView.showProgress();
                onRefresh();
            }
        });
        recyclerView.setRefreshListener(this);
        testingListAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, Object o) {
                TextingListModel textingListModel = (TextingListModel) o;
                boolean abnormal = (textingListModel.getAbnormalList() == null || textingListModel.getAbnormalList().size() == 0) ? false : true;
                startActivity(new Intent(TestingListActivity.this, TestingDetailsActivity.class)
                        .putExtra("userOrderId", textingListModel.getUserOrderId())
                        .putExtra("storeId", textingListModel.getStoreId())
                );
            }
        });
        onRefresh();

    }


    //获取车检单记录
    public void getDataList(final boolean isFrist) {

        OwneiInsListApi owneiInsListApi = new OwneiInsListApi(new HttpOnNextListener<TestingModel>() {

            @Override
            public void onNext(TestingModel o) {
                if (isFrist) {
                    testingListAdapter.clear();
                }
                if (o.getList().size() < 9) {
                    testingListAdapter.stopMore();

                }
                List<TextingListModel> list=o.getList();
                testingListAdapter.addAll(list);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
//                MyLog.e("yang", "-----");
//                recyclerView.showError();
            }
        }, this);
        owneiInsListApi.setSession(UserHelper.getInstance().getSession(this));
//        owneiInsListApi.setOwnerId(UserHelper.getInstance().getDefaultCar().getOwnerId() + "");
        owneiInsListApi.setPage(page + "");
        owneiInsListApi.setSize(10 + "");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(owneiInsListApi);
    }

    @Override
    protected void updateViews() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                if (!PhoneUtils.isNetworkConnected(TestingListActivity.this)) {
                    testingListAdapter.pauseMore();
                    recyclerView.showError();
                    return;
                }
                page++;
                getDataList(false);

            }
        }, 1000);
    }

    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                if (!PhoneUtils.isNetworkConnected(TestingListActivity.this)) {
                    testingListAdapter.pauseMore();
                    recyclerView.showError();
                    return;
                }
                page = 1;
                getDataList(true);
            }
        }, 1000);
    }
}
